package org.real.guetshop.service.impl;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.constant.MessageConstant;
import org.real.guetshop.constant.StatusConstant;
import org.real.guetshop.context.BaseContext;
import org.real.guetshop.exception.AccountIsExitException;
import org.real.guetshop.exception.AccountNotExitException;
import org.real.guetshop.exception.PasswordErrorException;
import org.real.guetshop.mapper.MerchantMapper;
import org.real.guetshop.pojo.dto.DetailDTO;
import org.real.guetshop.pojo.dto.MerchantDTO;
import org.real.guetshop.pojo.dto.MerchantLoginDTO;
import org.real.guetshop.pojo.dto.MerchantRegisterDTO;
import org.real.guetshop.pojo.entity.Store;
import org.real.guetshop.pojo.vo.StoreVO;
import org.real.guetshop.service.MerchantService;
import org.real.guetshop.utils.SHAUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class MerchantServiceImpl implements MerchantService {

    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 商家注册
     * @param storeRegisterDTO
     */
    @Override
    public void register(MerchantRegisterDTO storeRegisterDTO) {
        Store store = new Store();
        BeanUtils.copyProperties(storeRegisterDTO, store);

        //查找是否已经存在
        Store isExit = merchantMapper.getByName(store);

        if (isExit != null) {
            throw new AccountIsExitException(MessageConstant.ALREADY_EXISTS);
        }

        // 对商家密码进行加密处理
        SHAUtils.PasswordHash passwordHash = SHAUtils.encodePasswordWithSalt(storeRegisterDTO.getPassword());
        store.setPassword(passwordHash.getHash());
        store.setSalt(passwordHash.getSalt());

        store.setStatus(StatusConstant.ENABLE);

        //开始注册
        merchantMapper.insert(store);
    }

    /**
     * 商家登录
     * @param merchantLoginDTO
     * @return
     */
    @SneakyThrows
    @Override
    public Store login(MerchantLoginDTO merchantLoginDTO) {

        //判断是否存在
        Store store = merchantMapper.getByUsername(merchantLoginDTO);

        if (store == null) {
            throw new AccountNotExitException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

//        密码比对
        SHAUtils.PasswordHash passwordHash = new SHAUtils.PasswordHash(store.getPassword(), store.getSalt());

        if (!SHAUtils.verifyPassword(merchantLoginDTO.getPassword(),passwordHash)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return store;
    }

    /**
     * 商家信息补充
     * @param detailDTO
     */
    @Override
    public void detail(DetailDTO detailDTO) {
        merchantMapper.detail(detailDTO);
    }

    /**
     * 用户获取商家列表
     * @return
     */
    @Override
    public List<StoreVO> getMerchant() {
        String key = "user_merchant_list";
        //先从redis中查询
        List<StoreVO> list = (List<StoreVO>) redisTemplate.opsForValue().get(key);
        if (list != null) {
            //如果redis中有数据直接返回
            log.info("从redis中查询数据...");
            return list;
        }

        //没有数据再从数据库中查询
        list = merchantMapper.getMerchant();
        //将查到的数据存入redis中并且设置过期时间
        redisTemplate.opsForValue().set(key, list,5, TimeUnit.DAYS);
        return list;
    }

    /**
     * 商家信息修改
     * @param merchantDTO
     */
    @Override
    public void update(MerchantDTO merchantDTO) {
        Store store = new Store();
        BeanUtils.copyProperties(merchantDTO, store);
        store.setId(BaseContext.getCurrentId());
        merchantMapper.update(store);
    }

    /**
     * 商家获取信息
     * @return
     */
    @Override
    public StoreVO getStoreInfo(Integer storeId) {
        return merchantMapper.getStoreInfo(storeId);
    }
}
